<?php
/**
 * 后台菜单管理
 * @author cjli
 *
 */
class MenuAction extends AdminAction
{
	public function index()
	{
		import('COM.Tree');
		$tree = new Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$menudb = D('menu');
		$result = $menudb->order('listorder DESC, id ASC')->select();
		$array = array();
		foreach($result as $r) {
			$r['str_manage'] = '<a href="'.U('menu/add','parentid='.$r['id']).'">添加子菜单</a> | <a href="'.U('menu/edit','id='.$r['id']).'">修改</a> | <a href="javascript:confirmurl(\''.U('menu/delete','id='.$r['id']).'\',\'确认要删除 『'.$r['name'].' 』 吗？\')">删除</a> ';
			$array[] = $r;
		}
		$str  = "<tr>
					<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
					<td align='center'>\$id</td>
					<td >\$spacer\$name</td>
					<td align='center'>\$str_manage</td>
				</tr>";
		$tree->init($array);
		$categorys = $tree->get_tree(0, $str);
		$this->assign('categorys', $categorys);
		$this->display('menu');
	}
	
	public function add()
	{
		
		if(isset($_POST['dosubmit'])) {
			$parentid = intval($_POST['info']['parentid']);
			if(empty($_POST['info']['name'])){
				$this->error('菜单名称不能为空', U('menu/add', 'parentid='.$parentid));
			}
			$menudb = D('menu');
			$menudb->add($_POST['info']);
			$this->success('添加成功！', U('menu/add', 'parentid='.$parentid));
		}else{
			$parentid = isset($_GET['parentid']) ? intval($_GET['parentid']) : 0;
			import('COM.Tree');
			$tree = new Tree();
			$menudb = D('menu');
			$result = $menudb->select();
			$array = array();
			foreach($result as $r) {
				$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			
			$this->assign('show_validator', 1);
			$this->assign('select_categorys', $select_categorys);
			$this->display('menu');
		}
	}
	
	public function edit()
	{
		if(isset($_POST['dosubmit'])) {
			$id = intval($_POST['id']);
			if(empty($_POST['info']['name'])){
				$this->error('菜单名称不能为空！', U('menu/edit'. 'id='.$id));
			}
			$menudb = D('menu');
			$menudb->where('id='.$id)->save($_POST['info']);
			$this->success('操作成功！', U('menu/index'));
		}else{
			$menudb = D('menu');
			$id = intval($_GET['id']);
			$menu_info = $menudb->where(array('id'=>$id))->find();
			if($menu_info) extract($menu_info);
			$this->assign('menu', $menu_info);
			import('COM.Tree');
			$tree = new Tree();
			$result = $menudb->select();
			$array = array();
			foreach($result as $r) {
				$r['selected'] = $r['id'] == $menu_info['parentid'] ? 'selected' : '';
				$array[] = $r;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init($array);
			$select_categorys = $tree->get_tree(0, $str);
			
			$this->assign('show_validator', 1);
			$this->assign('select_categorys', $select_categorys);
			$this->display('menu');
		}
	}
	
	public function delete()
	{
		$id = intval($_GET['id']);
		$menudb = D('menu');
		$r = $menudb->where('parentid='.$id)->find();
		if($r){
			$this->error('操作失败，该菜单栏目下还有子菜单！', U('menu/index'));
		}else{
			$menudb->where('id='.$id)->delete();
			$this->success('操作成功！', U('menu/index'));
		}
	}
	
	public function listorder()
	{
		if(isset($_POST['dosubmit'])) {
			$menudb = D('menu');
			foreach($_POST['listorders'] as $id => $listorder) {
				$menudb->where(array('id'=>$id))->save(array('listorder'=>$listorder));
			}
			$this->success('排序操作成功！');
		} else {
			$this->error('排序操作失败！');
		}
	}
}